﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BCCommon.Enums;
using BCCommon.OAEnums;
using BCEntity.OA.TeamWorks.TaskRecord;

namespace BCData.OA.TeamWorks.TaskRecord
{
    /// <summary>
    /// 任务记录
    /// </summary>
    public interface ITaskRecordData
    {
        /// <summary>
        /// 检查任务记录信息是否存在
        /// </summary>
        /// <param name="taskRecordNo">任务记录编号</param>
        /// <returns></returns>
        Task<bool> CheckExists(string taskRecordNo);

        /// <summary>
        /// 获取任务记录信息
        /// </summary>
        /// <param name="taskRecordNo">任务记录编号</param>
        /// <returns></returns>
        Task<TaskRecordEntity> GetEntity(string taskRecordNo);

        /// <summary>
        /// 更新任务记录开始时间
        /// </summary>
        /// <param name="taskRecordNo">任务记录编号</param>
        /// <param name="startTime">开始时间</param>
        /// <returns></returns>
        Task<bool> UpdateStartTime(string taskRecordNo, DateTime? startTime);

        /// <summary>
        /// 新增一条任务记录信息
        /// </summary>
        /// <param name="entity">任务记录</param>
        /// <param name="receiverEntities">任务参与者</param>
        /// <returns></returns>
        Task<TaskRecordEntity> InsertAsync(TaskRecordEntity entity, IEnumerable<TaskReceiverRecordEntity> receiverEntities = null);

        /// <summary>
        /// 获取任务记录信息
        /// </summary>
        /// <param name="workflowActivityInstanceId">工作流活动实例Id</param>
        /// <returns></returns>
        Task<IEnumerable<TaskRecordEntity>> Get(long workflowActivityInstanceId);

        /// <summary>
        /// 更新任务执行人信息
        /// </summary>
        /// <param name="taskRecordEntity">任务执行人信息</param>
        /// <returns></returns>
        Task<bool> UpdateExecuterAdminInfo(TaskRecordEntity taskRecordEntity);

        /// <summary>
        /// 分页获取当前人员下的任务列表
        /// </summary>
        /// <param name="executerAdminId">执行人Id</param>
        /// <param name="start">开始日期</param>
        /// <param name="end">结束日期</param>
        /// <param name="handleStatus">处理状态</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="companyId">公司Id</param>
        /// <param name="ascending">是否升序排序</param>
        /// <param name="pageIndex">分页页码</param>
        /// <param name="pageSize">分页页数</param>
        /// <param name="taskStep">任务步骤</param>
        /// <param name="isMyApproved">是否我审批的</param>
        /// <returns></returns>
        Task<(IEnumerable<TaskRecordEntity>, int)> Query(long? executerAdminId, DateTime? start, DateTime? end, OATaskHandleStatus? handleStatus, string sortName, int? companyId, bool? ascending, int pageIndex, int pageSize, int? taskStep = null, bool? isMyApproved = null);

        /// <summary>
        /// 修改处理状态
        /// </summary>
        /// <param name="taskRecordNo">任务记录编号</param>
        /// <param name="handleStatus">处理状态</param>
        /// <returns></returns>
        Task<bool> UpdateHandleStatus(string taskRecordNo, OATaskHandleStatus handleStatus);

        /// <summary>
        /// 根据活动实例对应的业务单据编号查询任务记录信息
        /// </summary>
        /// <param name="applyCode">业务单据编号</param>
        /// <returns></returns>
        Task<IEnumerable<TaskRecordEntity>> GetTaskRecordEntitiesByReferenceNoAsync(string applyCode);

        /// <summary>
        /// 获取最近的一条任务单
        /// </summary>
        /// <param name="workflowActivityInstanceId"></param>
        /// <returns></returns>
        Task<TaskRecordEntity> GetLatestRecordAsync(long workflowActivityInstanceId);

        /// <summary>
        /// 根据活动实例Id获取任务列表
        /// </summary>
        /// <param name="workflowActivityInstanceId">活动实例Id</param>
        /// <param name="taskStep">任务步骤</param>
        /// <returns></returns>
        Task<IEnumerable<TaskRecordEntity>> GetByWorkflowActivityInstanceIdAsync(long workflowActivityInstanceId, int? taskStep = null);

        /// <summary>
        /// 根据关联业务单号和执行人查询任务信息
        /// </summary>
        /// <param name="referenceNo">关联业务单号</param>
        /// <param name="adminId">执行任务人</param>
        /// <returns></returns>
        Task<TaskRecordEntity> GetByReferenceNoAndTaskExecuterAdminId(string referenceNo, long adminId);

        /// <summary>
        /// 修改处理状态及备注
        /// </summary>
        /// <param name="taskRecordNo">任务记录编号</param>
        /// <param name="handleStatus">处理状态</param>
        /// <param name="taskRemark">任务备注</param>
        /// <returns></returns>
        Task<bool> UpdateHandleStatusAndTaskRemark(string taskRecordNo, OATaskHandleStatus handleStatus, string taskRemark);

        Task UpdateTaskRecordAsync(TaskRecordEntity lastTaskRecord);

        Task<(IEnumerable<TaskRecordEntity>, int)> GetPageableListAsync(string taskRecordNo, int? companyId, long? adminId, string[] workflowKeys, long? workflowActivityInstanceId, string referenceNumber, bool? isMyApproved, DateTime? startDate, DateTime? endDate, OATaskHandleStatus? status, string sortName, bool? ascending, int pageIndex, int pageSize);

        /// <summary>
        /// 获取今日待处理任务
        /// </summary>
        /// <param name="adminId">任务执行人ID</param>
        /// <param name="roleType">角色类型</param>
        /// <returns></returns>
        Task<IEnumerable<TaskRecordGroupWorkflowTypeEntity>> GetPendingTaskListAsync(long adminId, RoleType roleType);

        /// <summary>
        /// 获取我发起的任务列表
        /// </summary>
        /// <param name="adminId">任务执行人ID</param>
        /// <param name="roleType">角色类型</param>
        /// <returns></returns>
        Task<IEnumerable<TaskRecordGroupWorkflowTypeEntity>> GetAppliedTaskListAsync(long adminId, RoleType roleType);

        /// <summary>
        /// 我的任务和进度
        /// </summary>
        /// <param name="adminId">任务执行人ID</param>
        /// <param name="roleType">角色类型</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="handleStatus">处理状态</param>
        /// <param name="sortName">排序字段</param> 
        /// <param name="ascending">是否升序排序</param>
        /// <param name="pageIndex">分页页码</param>
        /// <param name="pageSize">分页页数</param>
        /// <returns></returns>
        Task<(IEnumerable<TaskRecordEntity>, int)> GetTaskRecordListAsync(long? adminId, RoleType? roleType, DateTime? startDate, DateTime? endDate, OATaskHandleStatus? status, string sortName, bool? ascending, int pageIndex, int pageSize);

        /// <summary>
        /// 获取待处理任务数
        /// </summary>
        /// <param name="adminId">任务执行人ID</param>
        /// <param name="roleType">角色类型</param>
        /// <param name="businessType">业务类型</param>
        /// <returns></returns>
        Task<int> GetPendingTaskCountByWorkFlowKeyAsync(long adminId, RoleType roleType, string businessType);

        /// <summary>
        /// 更新工作流指派错误，任务回滚到上一步
        /// </summary>
        /// <param name="currentTaskRecordNo">最新任务编号</param>
        /// <param name="backTaskRecordNo">回滚的任务编号</param>
        /// <param name="currentStep">回滚步骤</param>
        /// <param name="workflowActivityInstanceId">工作流活动实例Id</param>
        /// <returns></returns>
        Task<bool> UpdateRollBack(string currentTaskRecordNo, string backTaskRecordNo, int currentStep, long workflowActivityInstanceId);

        /// <summary>
        /// 修改处理状态
        /// </summary>
        /// <param name="taskRecordNoList">任务记录编号</param>
        /// <param name="handleStatus">处理状态</param>
        /// <returns></returns>
        Task<int> UpdateHandleStatus(List<string> taskRecordNoList, OATaskHandleStatus handleStatus);
    }
}
